package com.yanqu.road.server.manager.banquet;

import com.yanqu.road.entity.banquet.BanquetGiftInfo;
import com.yanqu.road.entity.banquet.BanquetInfo;
import com.yanqu.road.logic.bussiness.config.BanquetConfigBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 跨服宴会
 */
public class CrossBanquetConfigMgr extends TempMgr {

	private static Map<Integer, BanquetInfo> banquetInfoMap;

	private static Map<Integer, BanquetGiftInfo> banquetGiftInfoMap;

	private static RandomHelper threadSafeRandom = new RandomHelper();


	//两种宴会类型是不是同一个，判断能不能同时开启的
	public static boolean isTheSameType(int banquetInfoId1,int banquetInfoId2){
		BanquetInfo banquetInfo1 = banquetInfoMap.get(banquetInfoId1);
		BanquetInfo banquetInfo2 = banquetInfoMap.get(banquetInfoId2);
		if(banquetInfo1.getType1() == banquetInfo2.getType1()){
			return true;
		}
		if((banquetInfo1.getType1() == 1 || banquetInfo1.getType1() == 2) && (banquetInfo2.getType1() == 1 || banquetInfo2.getType1() == 2)){
			return true;
		}
		return false;
	}

	//判断宴会是不是跨服宴会
	public static boolean isCrossBanquet(int banquetInfoId){
		BanquetInfo banquetInfo = banquetInfoMap.get(banquetInfoId);
		return banquetInfo.getType1() == 4;
	}
	//判断宴会是不是跨服私人宴会
	public static boolean isCrossUserBanquet(int banquetInfoId) {
		BanquetInfo banquetInfo = banquetInfoMap.get(banquetInfoId);
		return banquetInfo.getType1() == 5;
	}

	//判断宴会是不是大佬生辰宴
	public static boolean isMogulBirthdayBanquet(int banquetInfoId) {
		BanquetInfo banquetInfo = banquetInfoMap.get(banquetInfoId);
		return banquetInfo.getType1() == 7;
	}

	//判断宴会是不是可以自己加入
	public static boolean canJoinSelf(int banquetInfoId) {
		BanquetInfo banquetInfo = banquetInfoMap.get(banquetInfoId);
		return banquetInfo.getType1() == 3 || banquetInfo.getType1() == 4;
	}

	//跨服盛宴人气礼奖励
	//1-10-101000=1;110091=1|2-20-101000=1;110091=1
	public static Map<Integer, List<String>> getCrossBanquetRewardMap() {
		Map<Integer, List<String>> res = new ConcurrentHashMap<>();
		List<String> levelStringList = StringUtils.stringToStringList(GameConfig.BANQUET_CROSS_POPULARITY_GIFT, "\\|");
		for (String levelString : levelStringList) {
			List<String> infoList = StringUtils.stringToStringList(levelString, "-");
			res.put(Integer.valueOf(infoList.get(0)), infoList);
		}
		return res;
	}

	//获取跨服盛宴的对应位置的对应奖励
	public static String getJoinReward(int pos,int giftId){
		BanquetGiftInfo banquetGiftInfo = banquetGiftInfoMap.get(giftId);
		if(banquetGiftInfo != null && !"0".equals(banquetGiftInfo.getJoinReward())){
			int max = -1;
			String reward = "";
			List<String> rewardList = StringUtils.stringToStringList(banquetGiftInfo.getJoinReward(),"\\|");
			for(String rewardInfo : rewardList){
				List<String> posRewardList = StringUtils.stringToStringList(rewardInfo,":");
				//最小位置以上的简历
				int paramPosMin = Integer.valueOf(posRewardList.get(0));
				if(pos >= paramPosMin && paramPosMin > max){
					max = paramPosMin;
					reward = posRewardList.get(1);
				}
			}
			return reward;
		}
		return "";
	}

	//宴会结束奖励
	public static String getEndReward(int pos,int banquetInfoId){
		BanquetInfo banquetInfo = banquetInfoMap.get(banquetInfoId);
		if(banquetInfo != null && !"0".equals(banquetInfo.getEndReward())){
			int max = -1;
			String reward = "";
			List<String> rewardList = StringUtils.stringToStringList(banquetInfo.getEndReward(),"\\|");
			for(String rewardInfo : rewardList){
				List<String> posRewardList = StringUtils.stringToStringList(rewardInfo,":");
				//最小位置以上的简历
				int paramPosMin = Integer.valueOf(posRewardList.get(0));
				if(pos >= paramPosMin && paramPosMin > max){
					max = paramPosMin;
					reward = posRewardList.get(1);
				}
			}
			return reward;
		}
		return "";
	}

	public static BanquetInfo getBanquetInfo(int banquetInfoId){
		return banquetInfoMap.get(banquetInfoId);
	}

	public static BanquetGiftInfo getBanquetGiftInfo(int banquetInfoId,int giftId){
		BanquetInfo banquetInfo = banquetInfoMap.get(banquetInfoId);
		BanquetGiftInfo banquetGiftInfo = banquetGiftInfoMap.get(giftId);
		if (banquetInfo == null || banquetGiftInfo == null) {
			return null;
		}
		if (banquetInfo.getType1() != banquetGiftInfo.getType1() || banquetInfo.getType2() != banquetGiftInfo.getType2()) {
			return null;
		}
		return banquetGiftInfo;
	}

	public static BanquetGiftInfo getBanquetGiftInfoByBanquetInfoId(int banquetInfoId) {
		BanquetInfo banquetInfo = banquetInfoMap.get(banquetInfoId);
		for (BanquetGiftInfo banquetGiftInfo : banquetGiftInfoMap.values()) {
			if (banquetInfo.getType1() == banquetGiftInfo.getType1() && banquetInfo.getType2() == banquetGiftInfo.getType2()) {
				return banquetGiftInfo;
			}
		}
		return null;
	}

	public static BanquetGiftInfo getBanquetGiftInfo(int giftId) {
		BanquetGiftInfo banquetGiftInfo = banquetGiftInfoMap.get(giftId);
		return banquetGiftInfo;
	}

	//获得相同type1类型且消耗一样的
	public static Map<Integer, BanquetGiftInfo> getBanquetGiftInfoSameConsume(int giftId) {
		Map<Integer, BanquetGiftInfo> giftMap = new ConcurrentHashMap<>();
		if (!banquetGiftInfoMap.containsKey(giftId)) {
			return giftMap;
		}
		BanquetGiftInfo banquetGiftInfo = banquetGiftInfoMap.get(giftId);
		Property ap = PropertyHelper.parseStringToProperty(banquetGiftInfo.getJoinConsume());
		for(BanquetGiftInfo giftInfo : banquetGiftInfoMap.values()){
			if(giftInfo.getType1() == banquetGiftInfo.getType1()){
				Property bp = PropertyHelper.parseStringToProperty(giftInfo.getJoinConsume());
				if(PropertyHelper.isTheSameGood(ap,bp)){
					giftMap.put(giftInfo.getId(),giftInfo);
				}
			}
		}
		return giftMap;
	}
	//通过消耗获取随礼
	public static Map<Integer,BanquetGiftInfo> getBanquetGiftByConsume(int type1,String consume){
		Map<Integer,BanquetGiftInfo> giftMap = new ConcurrentHashMap<>();
		Property ap = PropertyHelper.parseStringToProperty(consume);
		for(BanquetGiftInfo giftInfo : banquetGiftInfoMap.values()){
			if(giftInfo.getType1() == type1){
				Property bp = PropertyHelper.parseStringToProperty(giftInfo.getJoinConsume());
				if(PropertyHelper.isTheSameGood(ap,bp)){
					giftMap.put(giftInfo.getId(),giftInfo);
				}
			}
		}
		return giftMap;
	}

	//通过消耗获得礼物id，设置了自动回宴那个礼物不能拒绝别人赴宴，要找出来对应消耗的礼物
	public static BanquetGiftInfo getBanquetGiftByConsume(BanquetInfo banquetInfo,String consume){
		Property ap = PropertyHelper.parseStringToProperty(consume);
		for(BanquetGiftInfo giftInfo : banquetGiftInfoMap.values()){
			if(giftInfo.getType1() == banquetInfo.getType1() && giftInfo.getType2() == banquetInfo.getType2()){
				Property bp = PropertyHelper.parseStringToProperty(giftInfo.getJoinConsume());
				if(PropertyHelper.isTheSameGood(ap,bp)){
					return giftInfo;
				}
			}
		}
		return null;
	}

	public static boolean isServerNormalBanquet(int type) {
		BanquetInfo banquetInfo = banquetInfoMap.get(type);
		if(banquetInfo.getType1() == 1 || banquetInfo.getType1() == 2){
			return true;
		}
		return false;
	}

	public static boolean isServerFeastBanquet(int type) {
		BanquetInfo banquetInfo = banquetInfoMap.get(type);
		return banquetInfo.getType1() == 3;
	}

	//机器人触发条件
	public static Map<Integer,List<Integer>> getRobotCondtion(){
		Map<Integer,List<Integer>> res = new ConcurrentHashMap<>();
		List<String> tempList = StringUtils.stringToStringList(GameConfig.BANQUET_ROBOT_CONDITION,";");
		for(String process : tempList){
			List<String> processList = StringUtils.stringToStringList(process,":");
			res.put(Integer.valueOf(processList.get(0)),StringUtils.stringToIntegerList(processList.get(1),"-"));
		}
		return res;
	}

	//跨服机器人触发条件
	public static Map<Integer,List<Integer>> getCrossRobotCondition(){
		Map<Integer,List<Integer>> res = new ConcurrentHashMap<>();
		List<String> tempList = StringUtils.stringToStringList(GameConfig.CROSS_BANQUET_ROBOT_CONDITION,";");
		for(String process : tempList){
			List<String> processList = StringUtils.stringToStringList(process,":");
			res.put(Integer.valueOf(processList.get(0)),StringUtils.stringToIntegerList(processList.get(1),"-"));
		}
		return res;
	}

	public static Map<Integer, List<String>> getBanquetFeastRewardMap() {
		Map<Integer,List<String>> res = new ConcurrentHashMap<>();
		List<String> levelStringList = StringUtils.stringToStringList(GameConfig.BANQUET_FEAST_POPULARITY_GIFT,"\\|");
		for(String levelString : levelStringList){
			List<String> infoList = StringUtils.stringToStringList(levelString,"-");
			res.put(Integer.valueOf(infoList.get(0)),infoList);
		}
		return res;
	}

    @Override
	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean stop() {
		return false;
	}

	@Override
	public boolean save() {
		return false;
	}

	@Override
	public boolean reloadConfig() {
		banquetInfoMap = BanquetConfigBussiness.getBanquetInfoMap();
		banquetGiftInfoMap = BanquetConfigBussiness.getBanquetGiftInfoMap();
		return true;
	}

	@Override
	public boolean reloadData() throws Exception {
		return true;
	}
}
